<template>
  <div class="user_header" :style="{backgroundImage: `url(${background_image})`}">
    <van-icon name="set" class="user_set" @click="toSetting"/>
    <div class="user_avatar">
      <img :src="avatar" alt="头像" width="55" height="55">
    </div>
    <div class="user_info">
      <div>{{nickName}}</div>
      <div>{{levelName}} ID:{{userId}}</div>
      <div v-if="inviterId > 0">邀请人:{{inviterName}} (id:{{inviterId}})</div>
    </div>
  </div>
</template>

<script>
import avatar_default from '@/assets/images/avatar_default.png';
import bg_default from '@/assets/images/user_head_bg.png';
import {getLocalStorage, setLocalStorage} from '@/utils/local-storage';
import { userIndex } from '@/api/api';

export default {
  name: 'user-header',

  props: {
    isLogin: {
      type: Boolean,
      default: false
    }
  },
  created() {

  },
  data() {
    return {
      nickName: '昵称',
      avatar: avatar_default,
      background_image: bg_default,
      inviterId: 0,
      inviterName: '',
      userId: 0,
      userLevel: 0,
      levelName:'普通会员'
    };
  },

  activated() {
    this.getUserInfo();
    this.init();
  },

  methods: {
    init() {
      userIndex().then(res => {
        setLocalStorage({
          uncomment: res.data.data.order.uncomment,
          unpaid: res.data.data.order.unpaid,
          unrecv: res.data.data.order.unrecv,
          unship: res.data.data.order.unship,
          avatar: res.data.data.user.avatar,
          couponRate: res.data.data.user.couponRate,
          gender: res.data.data.user.gender,
          hasPayPassword: res.data.data.user.hasPayPassword,
          inviterId: res.data.data.user.inviterId,
          inviterName: res.data.data.user.inviterName,
          isVip: res.data.data.user.isVip,
          mobile: res.data.data.user.mobile,
          nickname: res.data.data.user.nickname,
          userId: res.data.data.user.userId,
          userLevel: res.data.data.user.userLevel,
          levelName: res.data.data.user.levelName,
          withdrawAmount: res.data.data.user.withdrawAmount,
          pendingAmount: res.data.data.user.pendingAmount,
          todayDirectAmount: res.data.data.user.todayDirectAmount,
          todayIndirectAmount: res.data.data.user.todayIndirectAmount,
          totalIndirectAmount: res.data.data.user.totalIndirectAmount,
          totalDirectAmount: res.data.data.user.totalDirectAmount,
          totalAmount: res.data.data.user.totalAmount,
          totalWithdrawAmount: res.data.data.user.totalWithdrawAmount
        });
      });
    },
    getUserInfo() {
      const infoData = getLocalStorage(
        'nickName','avatar', 'inviterId', 'inviterName', 'userLevel','userId'
      );
      this.avatar = infoData.avatar || avatar_default;
      this.nickName = infoData.nickName || '昵称';
      this.inviterId = infoData.inviterId || '';
      this.inviterName = infoData.inviterName || '';
      this.userLevel = infoData.userLevel || 0;
      this.userId = infoData.userId;
      if(this.userLevel == 1){
        this.levelName = '黄金会员';
      }else  if(this.userLevel == 2){
        this.levelName = '白金会员';
      }
    },
    toSetting() {
      this.$router.push({ name: 'user-information' });
    }
  }
};
</script>

<style lang="scss" scoped>
.user_header {
  background-repeat: no-repeat;
  background-size: cover;
  height: 130px;
  text-align: center;
  color: #fff;
  padding-top: 30px;
}

.user_set {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 24px;
}
.user_avatar {
  margin-bottom: 10px;
  margin-left: 15px;
  float: left;
  img {
    border: 0;
    border-radius: 50%;
  }
}
.user_info{
  margin-bottom: 10px;
  margin-left: 15px;
  float: left;
  div{
    padding: 1px;
    text-align: left;
  }
}
</style>
